﻿using System;
using System.Linq;
using ZSZ.DTO;
using Microsoft.EntityFrameworkCore;
using ZSZ.Application.IService;
using ZSZ.Core.Entities;
using ZSZ.EntityFramework;

namespace ZSZ.Application.Service
{
    public class AttachmentService : Repository<AttachmentEntity>, IAttachmentService
    {
        private AttachmentDTO ToDTO(AttachmentEntity att)
        {
            AttachmentDTO dto = new AttachmentDTO();
            dto.CreateDateTime = att.CreateDateTime;
            dto.IconName = att.IconName;
            dto.Id = att.Id;
            dto.Name = att.Name;
            return dto;
        }
        public AttachmentDTO[] GetAll()
        {
            var items = GetAllDataEntity();
            return items.ToList().Select(a => ToDTO(a)).ToArray();
        }

        public AttachmentDTO[] GetAttachments(long houseId)
        {
            var house = GetAllDataEntity().Include(a => a.HouseAttachments)
                .AsNoTracking().SingleOrDefault(h => h.Id == houseId);
            if (house == null)
            {
                throw new ArgumentException("houseId" + houseId + "不存在");
            }
            return house.HouseAttachments.ToList().Select(a => ToDTO(a.Attachment)).ToArray();
            
        }

        public AttachmentService(ZSZDbContext context) : base(context)
        {
        }
    }
}
